package com.bluebench.bigul.service;

import com.bluebench.bigul.domain.Element;
import com.bluebench.bigul.exception.GraphicInitializationException;
import com.bluebench.bigul.service.resolver.DefaultExpressionResolver;
import com.bluebench.bigul.service.resolver.ElementResolver;
import com.bluebench.bigul.service.resolver.GlobalElementResolver;
import com.bluebench.bigul.service.resolver.PrimitiveExpressionResolver;

public class TemplateParser {
	
	String templateXml;
	
	public Element parse() throws GraphicInitializationException{		
		
		ElementResolver eResolver = (ElementResolver)DefaultExpressionResolver.getInstance(DefaultExpressionResolver.ELEMENT_RESOLVER).build(templateXml);								 
		Element e = eResolver.getRoot(); 
		resolveGlobalElements();
		resolveTemplateElements(e);		
		return e;	
	}
	
	private void resolveGlobalElements() throws GraphicInitializationException{
		
		GlobalElementResolver globalResolver =  new GlobalElementResolver();
		globalResolver.resolve();
		
	}
	
	private void resolveTemplateElements(Element e) throws GraphicInitializationException{
		e.resolve();
		PrimitiveExpressionResolver primExprResolver = new PrimitiveExpressionResolver();
		primExprResolver.set(e);
		primExprResolver.resolve();
	}

	public String getTemplateXml() {
		return templateXml;
	}

	public void setTemplateXml(String templateXml) {
		this.templateXml = templateXml;
	}
	
	
	
	
}
